﻿/// <reference path="jquery-1.7.1-vsdoc.js" />
/// <reference path="/Scripts/SiteScript.js" />
jQuery(document).ready(function () {
    Initialize();
}
    );

//Model

var Form = new Object(); //Global Form Model

var SectorModel = function (id, obligatoryFieldID, rightFieldID, isValid) {

    this.ID = String(id);
    this.ObligatoryFieldID = String(obligatoryFieldID);
    this.RightFieldID = String(rightFieldID);
    this.TextBoxArray = new Array();
    this.IsValid = Boolean(isValid);

}

var TextBoxModel = function (id, clientID, stringWaterMark, isWaterMarked, validationType, isValid) {

    this.ID = String(id);
    this.ClientID = String(clientID);
    this.StringWaterMark = String(stringWaterMark);
    this.IsWaterMarked = Boolean(isWaterMarked);
    this.ValidationType = String(validationType);
    this.IsValid = Boolean(isValid);

}

//

//Initialize

function Initialize() {

    LoadControls();
    InitializeModel();
    AssignEventHandlers();

}

function InitializeModel() {

    //ID

    Form.SectorID = new SectorModel("ID", "[id$=ObligatoryFieldID]", "[id$=RightFieldID]",false);

    Form.TextBoxName = new TextBoxModel("TextBoxName", "[id$=TextBoxName]", "Dato obligatorio | Apellido | Empresa", true, "NoNull", false);
    Form.TextBoxSurname = new TextBoxModel("TextBoxSurname", "[id$=TextBoxSurname]", "", false, "NoNull", false);
    Form.TextBoxCompany = new TextBoxModel("TextBoxCompany", "[id$=TextBoxCompany]", "", false, "NoNull", false);

    Form.SectorID.TextBoxArray[0] = Form.TextBoxName;
    Form.SectorID.TextBoxArray[1] = Form.TextBoxSurname;
    Form.SectorID.TextBoxArray[2] = Form.TextBoxCompany;

    //

    //Contact

    Form.SectorContact = new SectorModel("Contact", "[id$=ObligatoryFieldContact]", "[id$=RightFieldContact]",false);

    Form.TextBoxEmail = new TextBoxModel("TextBoxEmail","[id$=TextBoxEmail]", "Dato obligatorio | Email válido | Teléfono", true, "Email", false);
    Form.TextBoxPhoneCode = new TextBoxModel("TextBoxPhoneCode", "[id$=TextBoxPhoneCode]", "Código de area", true, "PhoneNumeric", false);
    Form.TextBoxPhonePrefix = new TextBoxModel("TextBoxPhonePrefix", "[id$=TextBoxPhonePrefix]", "Prefijo", true, "PhoneNumeric", false);
    Form.TextBoxPhoneSuffix = new TextBoxModel("TextBoxPhoneSuffix", "[id$=TextBoxPhoneSuffix]", "Sufijo", true, "PhoneNumeric", false);

    Form.SectorContact.TextBoxArray[0] = Form.TextBoxEmail;
    Form.SectorContact.TextBoxArray[1] = Form.TextBoxPhoneCode;
    Form.SectorContact.TextBoxArray[2] = Form.TextBoxPhonePrefix;
    Form.SectorContact.TextBoxArray[3] = Form.TextBoxPhoneSuffix;

    //

    //Message

    Form.SectorMessage = new SectorModel("Message", "[id$=ObligatoryFieldMessage]", "[id$=RightFieldMessage]",false);

    Form.TextBoxSubject = new TextBoxModel("TextBoxSurname", "[id$=TextBoxSubject]", "(Sin asunto)", true, "None", true);
    Form.TextBoxMessage = new TextBoxModel("TextBoxMessage", "[id$=TextBoxMessage]", "Dato obligatorio", true, "NoNull", false);

    Form.SectorMessage.TextBoxArray[0] = Form.TextBoxSubject;
    Form.SectorMessage.TextBoxArray[1] = Form.TextBoxMessage;

    //

}

function LoadControls() {

    $(".FormCheckFields").css("display", "none");
    $(".FormRightField").css("display", "none");
    if ($(".FormMessageSended").css("display") != "none") {
        $(".FormMessageSended").fadeOut(3000);
    }
}

function ClearForm() {
    $(".FormTextBox").val("");
}

//

//Event Handlers

function AssignEventHandlers() {

    //ID

    $(Form["TextBoxName"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorID"], Form["TextBoxName"]); });
    $(Form["TextBoxSurname"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorID"], Form["TextBoxSurname"]); });
    $(Form["TextBoxCompany"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorID"], Form["TextBoxCompany"]); });

    $(Form["TextBoxName"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorID"], Form["TextBoxName"]); });
    $(Form["TextBoxSurname"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorID"], Form["TextBoxSurname"]); });
    $(Form["TextBoxCompany"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorID"], Form["TextBoxCompany"]); });

    $(Form["TextBoxName"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxName"], "In"); });

    $(Form["TextBoxName"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorID"], Form["TextBoxName"], "Out"); });

    //

    //Contact

    $(Form["TextBoxEmail"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorContact"], Form["TextBoxEmail"]); });
    $(Form["TextBoxPhoneCode"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorContact"], Form["TextBoxPhoneCode"]); });
    $(Form["TextBoxPhonePrefix"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorContact"], Form["TextBoxPhonePrefix"]); });
    $(Form["TextBoxPhoneSuffix"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorContact"], Form["TextBoxPhoneSuffix"]); });


    $(Form["TextBoxEmail"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorContact"], Form["TextBoxEmail"]); });
    $(Form["TextBoxPhoneCode"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorContact"], Form["TextBoxPhoneCode"]); });
    $(Form["TextBoxPhonePrefix"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorContact"], Form["TextBoxPhonePrefix"]); });
    $(Form["TextBoxPhoneSuffix"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorContact"], Form["TextBoxPhoneSuffix"]); });

    $(Form["TextBoxEmail"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxEmail"], "In"); });
    $(Form["TextBoxPhoneCode"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxPhoneCode"], "In"); });
    $(Form["TextBoxPhonePrefix"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxPhonePrefix"], "In"); });
    $(Form["TextBoxPhoneSuffix"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxPhoneSuffix"], "In"); });

    $(Form["TextBoxEmail"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorContact"], Form["TextBoxEmail"], "Out"); });
    $(Form["TextBoxPhoneCode"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorContact"], Form["TextBoxPhoneCode"], "Out"); });
    $(Form["TextBoxPhonePrefix"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorContact"], Form["TextBoxPhonePrefix"], "Out"); });
    $(Form["TextBoxPhoneSuffix"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorContact"], Form["TextBoxPhoneSuffix"], "Out"); });

    //

    //Message

    $(Form["TextBoxSubject"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorMessage"], Form["TextBoxSubject"]); });
    $(Form["TextBoxMessage"]["ClientID"]).keyup(function () { TextBox_Keyup(this, Form["SectorMessage"], Form["TextBoxMessage"]); });

    $(Form["TextBoxSubject"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorMessage"], Form["TextBoxSubject"]); });
    $(Form["TextBoxMessage"]["ClientID"]).change(function () { TextBox_Change(this, Form["SectorMessage"], Form["TextBoxMessage"]); });

    $(Form["TextBoxSubject"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxSubject"], "In"); });
    $(Form["TextBoxMessage"]["ClientID"]).focusin(function () { TextBox_Focusin(this, Form["TextBoxMessage"], "In"); });

    $(Form["TextBoxSubject"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorMessage"], Form["TextBoxSubject"], "Out"); });
    $(Form["TextBoxMessage"]["ClientID"]).focusout(function () { TextBox_Focusout(this, Form["SectorMessage"], Form["TextBoxMessage"], "Out"); });

    //

    //Send

    $("[id$=ButtonSend]").click(function () { return ButtonSend_Click(this); });

    //

}

function TextBox_Keyup(textbox, sector, textboxModel) {
    ValidTextBox(textbox, textboxModel);
    ValidSector(sector);
    ShowSectorState(sector);

}

function TextBox_Change(textbox, sector, textboxModel) {
    ValidTextBox(textbox, textboxModel);
    ValidSector(sector);
    ShowSectorState(sector);

}

function TextBox_Focusin(textbox,textboxModel,focusType) {
    ValidWaterMark(textbox, textboxModel, focusType);
}

function TextBox_Focusout(textbox, sector, textboxModel, focusType) {
    ValidTextBox(textbox, textboxModel);
    ValidWaterMark(textbox, textboxModel, focusType);
    ValidSector(sector);
    ShowSectorState(sector);
}

function ButtonSend_Click(buttonSended) {

    var validForm = ValidForm();
    ShowFormState(validForm);
    if (validForm) {
        return true;
    }
    else {
        return false;
    }


}



//

//Validations

function ValidWaterMark(textbox, textboxModel, focusType) {
    
    switch (focusType) {
        case "In":
            if (textboxModel["IsWaterMarked"]) {
                $(textbox).val("");
                textboxModel["IsWaterMarked"] = false;
            }
            break;
        case "Out":
            if (ValueIsNull($(textbox).val()) && !textboxModel["IsWaterMarked"]) {
                $(textbox).val(textboxModel["StringWaterMark"]);
                textboxModel["IsWaterMarked"] = true;
            }
            break;
    }
}


function ValidTextBox(textbox, textboxModel) {
    switch (textboxModel["ValidationType"]) {
        case "None":
            Form[textboxModel["ID"]]["IsValid"] = true;
            break;
        case "NoNull":
            if (!ValueIsNull($(textbox).val())) {

                Form[textboxModel["ID"]]["IsValid"] = true;
            }
            else {

                Form[textboxModel["ID"]]["IsValid"] = false;
            }
            break;
        case "Email":
            if (ValueIsEmail($(textbox).val())) {
                Form[textboxModel["ID"]]["IsValid"] = true;
            }
            else {

                Form[textboxModel["ID"]]["IsValid"] = false;
            }
            break;
        case "PhoneNumeric":
            if (ValueIsPhoneNumeric($(textbox).val())) {
                Form[textboxModel["ID"]]["IsValid"] = true;
            }
            else {

                Form[textboxModel["ID"]]["IsValid"] = false;
            }
            break;
    }

}

function ValidSector(sector) {

    switch (sector["ID"]) {

        case "ID": //Logica: Valida que al menos uno de los textbox de este sector sea no nulo
            var isValid = false;
            var i = 0;
            for (textbox in Form["SectorID"]["TextBoxArray"]) {
                var textboxModel = Form["SectorID"]["TextBoxArray"][i];
                isValid = isValid || textboxModel["IsValid"];
                i++;
            }
            if (isValid) {
                Form["SectorID"]["IsValid"] = true;
            }
            else {
                Form["SectorID"]["IsValid"] = false;
            }

            break;

        case "Contact": //Logica: Valida email y telefono y verifica que al menos uno de los dos campos este completo
            var validEmail = Boolean;
            var emailIsWaterMarked = Boolean;
            var validPhone = Boolean;
            var phoneIsWaterMarked = true;

            validEmail = Form["SectorContact"]["TextBoxArray"][0]["IsValid"];
            emailIsWaterMarked = (Form["SectorContact"]["TextBoxArray"][0]["StringWaterMark"] == $(Form["SectorContact"]["TextBoxArray"][0]["ClientID"]).val());

            if (emailIsWaterMarked) {
                validEmail = true;
            }

            for (var i = 1; i <= 3; i++) {
                validPhone = validPhone && Form["SectorContact"]["TextBoxArray"][i]["IsValid"];
            }

            for (var i = 1; i <= 3; i++) {
                phoneIsWaterMarked = phoneIsWaterMarked && Form["SectorContact"]["TextBoxArray"][i]["IsWaterMarked"];
            }
            if (phoneIsWaterMarked) {
                validPhone = true; //A pesar de que la validacion del textbox este en false si los tres campos estan con marca de agua es valido
            }
            if (
            (validEmail && validPhone)
            &&
            (!emailIsWaterMarked || !phoneIsWaterMarked)
            ) {
                Form["SectorContact"]["IsValid"] = true;
            }
            else {
                Form["SectorContact"]["IsValid"] = false;
            }

            break;
        case "Message":
            validMessage = Form["TextBoxMessage"]["IsValid"];
            if (validMessage) {
                Form["SectorMessage"]["IsValid"] = true;
            }
            else {
                Form["SectorMessage"]["IsValid"] = false;
            }

            break;
    }
}

function ValidForm() {
    if (Form["SectorID"]["IsValid"]
    && Form["SectorContact"]["IsValid"]
    && Form["SectorMessage"]["IsValid"]) {
        return true;
    }
    return false;
}


//

//Interface

function ShowSectorState(sector) {

    if (sector["IsValid"]) {
        $(sector["ObligatoryFieldID"]).fadeOut(0);
        $(sector["RightFieldID"]).fadeIn(500);
    }
    else {
        $(sector["RightFieldID"]).fadeOut(0);
        $(sector["ObligatoryFieldID"]).fadeIn(500);
    }

}

function ShowFormState(isValid) {
    $("[id$=CheckFields]").fadeOut(0);
    $("[id$=MessageSended]").fadeOut(0); 
    if (isValid) {
        $("[id$=CheckFields]").fadeOut(0);
        $("[id$=MessageSended]").fadeIn(0); //testear efecto 
    }
    else {
        $("[id$=MessageSended]").fadeOut(0); 
        $("[id$=CheckFields]").fadeIn(500);
    }
}

//